//
//  BaseNavController.m
//  1243
//
//  Created by 我的 on 16/3/22.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "BaseNavigationController.h"

@interface BaseNavigationController ()<UINavigationControllerDelegate>

@property (nonatomic, strong) id popDelegate;

@end

@implementation BaseNavigationController

+ (void)initialize {
    //获取当前类下面的UIBarButtonItem
    UIBarButtonItem *item = [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[self]];
    
    //设置导航条按钮颜色
    NSMutableDictionary *titleAttrDicM = [NSMutableDictionary dictionary];
    titleAttrDicM[NSForegroundColorAttributeName] = [UIColor whiteColor];
    [item setTitleTextAttributes:titleAttrDicM forState:UIControlStateNormal];
    [item setTintColor:[UIColor whiteColor]];
    //注意导航条上按钮不可用，用模型的文字属性设置是不好使的
    //设置不可用状态下的颜色
    //    titleAttrDicM = [NSMutableDictionary dictionary];
    //    titleAttrDicM[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
    //    [item setTitleTextAttributes:titleAttrDicM forState:UIControlStateDisabled];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.popDelegate = self.interactivePopGestureRecognizer.delegate;
    self.delegate = self;
    
    self.navigationBar.barTintColor = DEFAULT_COLOR;
    self.navigationBar.translucent = NO;
    
    //    self.navigationItem.backBarButtonItem.tintColor = [UIColor whiteColor];
    [self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil]];
    [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
}

//导航控制器跳转完成的时候调用
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    //    WSYLog(@"%@", self.viewControllers[0]);
    if (viewController == self.viewControllers[0]) {
        //显示根控制器
        //还原滑动返回手势的代理
        self.interactivePopGestureRecognizer.delegate = self.popDelegate;
        
    } else {
        //实现滑动返回功能
        //清空滑动返回手势的代理，就能实现滑动返回
        self.interactivePopGestureRecognizer.delegate = nil;
    }
    //    WSYLog(@"%s", __func__);
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    //    WSYLog(@"%ld", self.viewControllers.count);
    
    //设置非根控制器导航条的内容
    if (self.viewControllers.count != 0) {
        //非根控制器
        //设置导航条的内容
        //设置导航条左边和右边
        //如果把导航条上的返回按钮覆盖，滑动返回功能就没有了
        
        //左边
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"jiantou"] highImage:[UIImage imageNamed:@"jiantou"] target:self action:@selector(backToPre) forControlEvents:UIControlEventTouchUpInside title:nil];
        viewController.navigationItem.leftBarButtonItem.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        
    }
    
    [super pushViewController:viewController animated:animated];
}

- (void)backToPre {
    [self popViewControllerAnimated:YES];
}


@end
